package com.ph.website.home.service.impl;

import com.ph.website.home.base.common.CommonResult;
import com.ph.website.home.base.common.DataGrid;
import com.ph.website.home.base.common.Ref;
import com.ph.website.home.base.utils.ServletUtil;
import com.ph.website.home.base.utils.TimeUtil;
import com.ph.website.home.entity.HomeCert;
import com.ph.website.home.entity.HomeImg;
import com.ph.website.home.entity.News;
import com.ph.website.home.entity.NewsContent;
import com.ph.website.home.mapper.HomeCertMapper;
import com.ph.website.home.mapper.HomeImgMapper;
import com.ph.website.home.mapper.NewsContentMapper;
import com.ph.website.home.mapper.NewsMapper;
import com.ph.website.home.param.HelpSupportParam;
import com.ph.website.home.param.NewsParam;
import com.ph.website.home.service.IAddressService;
import com.ph.website.home.service.IHelpSupportService;
import com.ph.website.home.service.INewsService;
import com.ph.website.home.service.IWebService;
import com.ph.website.home.vo.AddressVo;
import com.ph.website.home.vo.HelpSupportVo;
import com.ph.website.home.vo.HomeImgVo;
import com.ph.website.home.vo.NewsVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WebServiceImpl implements IWebService {

    @Resource
    private IAddressService addressService;
    @Resource
    private IHelpSupportService helpSupportService;
    @Resource
    private INewsService newsService;
    @Resource
    private NewsMapper newsMapper;
    @Resource
    private NewsContentMapper newsContentMapper;
    @Resource
    private HomeImgMapper homeImgMapper;
    @Resource
    private HomeCertMapper homeCertMapper;

    /**关于我们*/
    public void about(){
        HttpServletRequest request = ServletUtil.getRequest();
        CommonResult<AddressVo.GetVo> commonResult = addressService.get();
        AddressVo.GetVo data = commonResult.getData();
        request.setAttribute("commonData",data);
    }

    /**前端获取帮助支持列表*/
    public CommonResult helpList(HelpSupportParam.ListParam param){

        param.setHelpState(1);
        CommonResult<DataGrid<HelpSupportVo.ListVo>> result = helpSupportService.list(param);

        List<HelpSupportVo.ListVo> rows = result.getData().getRows();

        List<HelpSupportVo.WebListVo> vos = new ArrayList<>();
        for(HelpSupportVo.ListVo help : rows){
            HelpSupportVo.WebListVo vo = new HelpSupportVo.WebListVo();
            vo.setA(help.getAnswer());
            vo.setQ(help.getQuestion());
            vos.add(vo);
        }

        DataGrid<HelpSupportVo.WebListVo> webDataGrid = new DataGrid<>();
        webDataGrid.setRows(vos);
        webDataGrid.setTotal(result.getData().getTotal());

        return Ref.ok(webDataGrid);
    }

    /**前端获取新闻列表*/
    public CommonResult newsList(NewsParam.ListParam param){
        param.setNewsState(3);
        CommonResult<DataGrid<NewsVo.ListVo>> result = newsService.list(param);
        List<NewsVo.ListVo> rows = result.getData().getRows();

        List<NewsVo.WebListVo> vos = new ArrayList<>();

        for(NewsVo.ListVo news : rows){
            NewsVo.WebListVo vo = new NewsVo.WebListVo();
            vo.setTitle(news.getTitle());
            vo.setTime(news.getPublishTime().substring(0,10));
            vo.setNewsId(news.getNewsId());
            vo.setUrl("/detail.html-"+news.getNewsId());
            vos.add(vo);
        }
        DataGrid<NewsVo.WebListVo> webDataGrid = new DataGrid<>();
        webDataGrid.setRows(vos);
        webDataGrid.setTotal(result.getData().getTotal());
        return Ref.ok(webDataGrid);

    }

    /**前端获取新闻详情*/
    public void detail(Integer newsId){

        HttpServletRequest request = ServletUtil.getRequest();

        News news = newsMapper.selectById(newsId);
        NewsContent newsContent = newsContentMapper.getByNewsId(newsId);
        String title = news.getTitle();
        String origin = news.getOrigin();
        String content = newsContent.getContent();
        String time = TimeUtil.formaterYYYYMMdd(news.getPublishTime());

        int hasNext = 0;
        int hasLast = 0;
        int nextId = 0;
        int lastId = 0;
        News nextNews = newsMapper.next(news.getOrderNum(), news.getNewsType());
        News lastNews = newsMapper.last(news.getOrderNum(), news.getNewsType());
        if(nextNews==null){
            hasNext = 0;
        }else{
            hasNext = 1;
            nextId = nextNews.getId();
        }

        if(lastNews==null){
            hasLast = 0;

        }else{
            hasLast = 1;
            lastId = lastNews.getId();
        }

        Map<String, Object> map = new HashMap<>();
        map.put("title", title);
        map.put("origin", origin);
        map.put("content", content);
        map.put("time", time);
        //上一篇
        map.put("hasLast", hasLast);
        map.put("lastId", lastId);
        //下一篇
        map.put("hasNext", hasNext);
        map.put("nextId", nextId);

        //新闻类型
        map.put("newsType", news.getNewsType());

        request.setAttribute("data", map);
    }

    /**前端首页获取轮播图和证书图*/
    public void index(){
        HttpServletRequest request = ServletUtil.getRequest();

        List<HomeImg> homeImgs = homeImgMapper.listUsable();

        List<HomeImgVo.WebListVo> vos = new ArrayList<>();

        int i = 0;
        for(HomeImg img : homeImgs){
            i++;
            HomeImgVo.WebListVo vo = new HomeImgVo.WebListVo();
            vo.setImgUrl(img.getImgUrl());
            vo.setTitle(img.getTitle());
            vo.setSubTitle1(img.getSubTitle1());
            vo.setSubTitle2(img.getSubTitle2());
            vo.setSubTitle3(img.getSubTitle3());
            vo.setOrderNum(i);
            vo.setHasLink(img.getHasLink());
            vo.setLinkUrl(img.getLinkUrl());
            vos.add(vo);
        }

        request.setAttribute("homeImgs", vos);

        List<HomeCert> homeCerts = homeCertMapper.listUsable();
        request.setAttribute("homeCerts", homeCerts);
    }
}
